export class TimeUtils {
    /**获取当前的秒数 */
    public static GetTimeBySecond(): number {
        let date = new Date;
        let datetime = Math.floor(date.getTime() / 1000);
        return datetime;
    }

        /**
     * 格式化时间为 HH:MM:SS
     * @param second_num 总秒数
     * @param showHours 是否显示小时（默认自动判断）
     */
    public static SecondToHours(second_num: number, showHours?: boolean): string {
        second_num = Math.floor(second_num);
        const hours = Math.floor(second_num / 3600);
        const minutes = Math.floor((second_num % 3600) / 60);
        const seconds = second_num % 60;

        // 自动判断是否需要显示小时
        const shouldShowHours = showHours ?? hours > 0;

        // 使用padStart统一处理补零
        const h = String(hours).padStart(2, '0');
        const m = String(minutes).padStart(2, '0');
        const s = String(seconds).padStart(2, '0');

        return shouldShowHours ? `${h}:${m}:${s}` : `${m}:${s}`;
    }

    /**格式化时间 年/月/日 */
    public static FormatDate1(time: number, split: string = "/"): string {
        let data = new Date(time)
        return data.getFullYear() + split + (data.getUTCMonth() + 1) + split + data.getUTCDate()
    }
    /**获取过去了多少时间 30分钟前 */
    public static BeforeTime(time) {
        if (time == null) {
            return "";
        }
        let date = new Date;
        let datetime = Math.floor(date.getTime() / 1000);
        let second_num = datetime - time;
        if (second_num <= 0) {
            second_num = 1;
        }
        let time_str;
        if (Math.floor(second_num / 60 / 60 / 24) > 0) //天
        {
            time_str = Math.floor(second_num / 60 / 60 / 24) + "天前";
        } else if (Math.floor(second_num / 60 / 60) > 0) //小时
        {
            time_str = Math.floor(second_num / 60 / 60) + "小时前";
        } else if (Math.floor(second_num / 60) > 0) //分钟
        {
            time_str = Math.floor(second_num / 60) + "分钟前";
        } else //秒 
        {
            time_str = Math.floor(second_num) + "秒前";
        }
        return time_str;
    }

    /**比较是否是当天 */
    public static compareIsToday(time: number): boolean {
        let src = new Date(time * 1000)
        let src_year = src.getFullYear()
        let src_mo = src.getUTCMonth()
        let src_day = src.getUTCDay()
        let cur = new Date(this.GetTimeBySecond() * 1000)
        let cur_year = cur.getFullYear()
        let cur_mo = cur.getUTCMonth()
        let cur_day = cur.getUTCDay()
        if (src_year == cur_year && src_mo == cur_mo && src_day == cur_day) {
            return true;
        }
        return false;
    }
    /**超出几天 */
    public static overDay(time: number): number {
        let src = new Date(time)
        let src_year = src.getFullYear()
        let src_mo = src.getUTCMonth()
        let src_day = src.getUTCDay()
        let cur = new Date(this.GetTimeBySecond() * 1000)
        let cur_year = cur.getFullYear()
        let cur_mo = cur.getUTCMonth()
        let cur_day = cur.getUTCDay()
        if (src_year == cur_year && src_mo == cur_mo) {
            return cur_day - src_day;
        }
        return 30;
    }

    /**获取今天开始时间 */
    public static dayStart(): number {
        let daySart = new Date(new Date(new Date().toLocaleDateString()).getTime())
        return daySart.getTime()
    }
    /**获取今天结束时间 */
    public static dayEnd(): number {
        let dayEnd = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1)
        return dayEnd.getTime();
    }
    /**相差了几个小时 */
    public static getDiffHours(time: number): number {
        let curDate = new Date()
        let chours = curDate.getHours()
        let srcDate = new Date(time * 1000)
        let srcHours = srcDate.getHours()

        return chours - srcHours
    }
    /**得到指定时间 */
    public static getSplaceTime(hour: number, min: number): number {
        let date = new Date()
        //需要得到第二天的
        if (date.getHours() >= hour) {
            date.setHours(hour + 24)
        } else {
            date.setHours(hour)
        }
        date.setMinutes(min)
        date.setSeconds(0)
        return Number((date.getTime() / 1000).toFixed(0))
    }

}